const mongoose = require('mongoose');

const modelSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        trim: true
    },
    description: {
        type: String,
        default: ''
    },
    userId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    geometry: {
        type: {
            type: String,
            enum: ['cube', 'sphere', 'cylinder', 'cone', 'torus', 'plane', 'custom', 'text3d', 'lathe', 'extrude', 'tube', 'group', 'external_model'],
            default: 'cube'
        },
        color: {
            type: String,
            default: '#FFFFFF' // 默认白色
        },
        material: {
            type: {
                type: String,
                enum: ['basic', 'standard', 'phong', 'lambert', 'normal', 'physical'],
                default: 'standard'
            },
            wireframe: { type: Boolean, default: false },
            metalness: { type: Number, default: 0.1 },
            roughness: { type: Number, default: 0.5 },
            opacity: { type: Number, default: 1.0 },
            transparent: { type: Boolean, default: false },
            texture: { type: String, default: null }
        },
        dimensions: {
            width: { type: Number, default: 1 },
            height: { type: Number, default: 1 },
            depth: { type: Number, default: 1 },
            radius: { type: Number, default: 0.5 }, // 用于球体、圆柱体等
            tube: { type: Number, default: 0.2 },   // 用于圆环
            segments: { type: Number, default: 32 }, // 用于确定曲面的精细程度
            text: { type: String, default: "" },     // 用于3D文本
            font: { type: String, default: "helvetiker" }, // 用于3D文本的字体
            points: { type: Array, default: [] },    // 用于自定义形状的点
            extrude: {                               // 用于挤出几何体
                steps: { type: Number, default: 1 },
                depth: { type: Number, default: 1 },
                bevelEnabled: { type: Boolean, default: false },
                bevelThickness: { type: Number, default: 0.2 },
                bevelSize: { type: Number, default: 0.1 },
                bevelSegments: { type: Number, default: 3 }
            }
        },
        position: {
            x: { type: Number, default: 0 },
            y: { type: Number, default: 0 },
            z: { type: Number, default: 0 }
        },
        rotation: {
            x: { type: Number, default: 0 },
            y: { type: Number, default: 0 },
            z: { type: Number, default: 0 }
        },
        scale: {
            x: { type: Number, default: 1 },
            y: { type: Number, default: 1 },
            z: { type: Number, default: 1 }
        },
        // 支持自定义几何体的顶点和面
        customGeometry: {
            vertices: [{ type: Array }],  // 顶点数组 [x, y, z]
            faces: [{ type: Array }],     // 面数组 [a, b, c] 表示顶点索引
            normals: [{ type: Array }]    // 法线数组
        },
        // 支持布尔运算
        booleanOperation: {
            type: { type: String, enum: ['union', 'subtract', 'intersect'], default: null },
            targetId: { type: mongoose.Schema.Types.ObjectId, default: null } // 目标几何体的ID
        },
        // 支持组合多个几何体
        children: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Model3D' }]
    },
    // 添加额外几何体对象数组
    additionalObjects: [{
        type: {
            type: String,
            enum: ['cube', 'sphere', 'cylinder', 'cone', 'torus', 'plane', 'custom', 'text3d', 'lathe', 'extrude', 'tube', 'external_model'],
            required: true
        },
        color: {
            type: String,
            default: '#FFFFFF'
        },
        material: {
            type: {
                type: String,
                enum: ['basic', 'standard', 'phong', 'lambert', 'normal', 'physical'],
                default: 'standard'
            },
            wireframe: { type: Boolean, default: false },
            metalness: { type: Number, default: 0.1 },
            roughness: { type: Number, default: 0.5 },
            opacity: { type: Number, default: 1.0 },
            transparent: { type: Boolean, default: false }
        },
        dimensions: {
            width: { type: Number },
            height: { type: Number },
            depth: { type: Number },
            radius: { type: Number },
            tube: { type: Number },
            segments: { type: Number },
            text: { type: String },
            font: { type: String },
            points: { type: Array }
        },
        position: {
            x: { type: Number, default: 0 },
            y: { type: Number, default: 0 },
            z: { type: Number, default: 0 }
        },
        rotation: {
            x: { type: Number, default: 0 },
            y: { type: Number, default: 0 },
            z: { type: Number, default: 0 }
        },
        scale: {
            x: { type: Number, default: 1 },
            y: { type: Number, default: 1 },
            z: { type: Number, default: 1 }
        }
    }],
    file: {
        originalName: String,
        fileName: String,
        fileSize: Number,
        fileType: String,
        filePath: String,
        uploadDate: {
            type: Date,
            default: null
        }
    },
    thumbnail: {
        path: String,
        base64: String,
        generateDate: {
            type: Date,
            default: null
        }
    },
    // 导出设置
    exportSettings: {
        format: { type: String, enum: ['stl', 'obj', 'gltf', 'glb'], default: 'stl' },
        quality: { type: String, enum: ['low', 'medium', 'high'], default: 'medium' }
    },
    // 共享和权限设置
    sharing: {
        isPublic: { type: Boolean, default: false },
        sharedWith: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }]
    },
    createdAt: {
        type: Date,
        default: Date.now
    },
    updatedAt: {
        type: Date,
        default: Date.now
    },
    // 这里可以根据需要添加更多字段，例如模型文件路径、预览图等
});

// 更新时自动更新updatedAt字段
modelSchema.pre('findOneAndUpdate', function () {
    this.set({ updatedAt: Date.now() });
});

const Model3D = mongoose.model('Model3D', modelSchema);

module.exports = Model3D; 